package com.lj.ljminsu.houseDetails.service.impl;

import com.google.gson.Gson;
import com.lj.ljminsu.houseDetails.DTO.HouseDetailDTO;
import com.lj.ljminsu.houseDetails.bean.*;
import com.lj.ljminsu.houseDetails.dao.HouseDetailDao;
import com.lj.ljminsu.houseDetails.service.HouseDetailService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class HouseDetailServiceImpl implements HouseDetailService {
    @Resource
    private HouseDetailDao houseDetailDao;
    //可抢购房屋数量
    //房子某天抢购前的房数
    @Override
    public Integer queryHouseNum(Integer houseId, String start) {
        //价格-房数修改表中的房间数量
        Integer houseNum = houseDetailDao.queryHouseNum(houseId, start);
        if (houseNum == null){
            //未修改过,基础数量
            houseNum = houseDetailDao.queryHouseRoomByid(houseId).getBaseRoomNum();
        }
        return houseNum;
    }

    //详情页所有房源信息
    @Override
    public String queryHouseDetails(Integer houseId) {
        /**
         * //获取日历
         */
        //当前时间:2019-02-01
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String s = dateFormat.format(date);
        //基础房价,房数
        HouseBase houseBase = houseDetailDao.queryHouseRoomByid(houseId);
        //当前房源的优惠方案
        List<HouseReduceSku> reduceSkus = houseDetailDao.queryReduceListById(houseId);
        //某些日期的房源价格,数量
        List<HouseStatus> houseStatuses = houseDetailDao.queryHouseStatusById(houseId,s);
        //返回到页面的是当前日期到90天后的初步信息显示
        ConcurrentHashMap<String, HouseStatus> concurrentHashMap = new ConcurrentHashMap<>();
        //为之后的每一天添加信息
        Calendar c = Calendar.getInstance();//获取日历对象
        HouseStatus houseStatus = new HouseStatus(String.valueOf(houseBase.getBaseRoomPrice()),
                String.valueOf(houseBase.getBaseRoomNum()));//基础房价,数量
        int i = 1;
        while (i<=90){
            String everydate = dateFormat.format(c.getTime());
            concurrentHashMap.put(everydate,houseStatus);
            c.add(Calendar.DATE,1);//在当前日期每循环一次,加一天
            i++;
        }
        for (HouseStatus status:houseStatuses
        ) {
            //已添加完的集合中有更新过的键(日期),覆盖成新的值
            concurrentHashMap.put(status.getDates(),status);
        }


        String json = new Gson().toJson(concurrentHashMap);

        return json;
    }
    //查询房源相关信息
    @Override
    public String queryHouseInfo(Integer houseId){
        /**
         * 房源信息
         */
        HouseBaseInfo houseBaseInfo = houseDetailDao.queryHouseBaseInfoById(houseId);//房间基础信息
        HouseRoomDesc houseRoomDesc = houseDetailDao.queryRoomDescById(houseId);//房间描述
        String services = houseBaseInfo.getServices();
        String[] split = services.split(",");
        List<Object> asList = Arrays.asList(split);
        List<HouseService> houseServices = houseDetailDao.queryHouseServiceListById(asList);//所含有的服务
        List<Imgs> imgs = houseDetailDao.queryHouseImgListById(houseId);

        HouseDetailDTO houseDetailDTO = new HouseDetailDTO();
        houseDetailDTO.setHouseBaseInfo(houseBaseInfo);
        houseDetailDTO.setHouseRoomDesc(houseRoomDesc);
        houseDetailDTO.setHouseServices(houseServices);
        houseDetailDTO.setImgs(imgs);
        String json = new Gson().toJson(houseDetailDTO);
        return json;
    }
}
